﻿using ResLibary;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//namespace ResLibary
//{
public class ResourceObj
{
    /// <summary>
    /// 引用次数
    /// </summary>
    public int quoteNum;

    public string objName;

    public UnityEngine.Object obj;
}
[System.Serializable]
public class ResourceSettingStateObj
{
    public string m_Name;
    public string m_ResourcePath;
    public string m_Type;
    public string m_AssetPath;
    public AssetExistStatusEnum m_ExistStatus = AssetExistStatusEnum.Quote;
}
public class ResourceStateObj : ResourceSettingStateObj
{
    /// <summary>
    /// 引用次数
    /// </summary>
    public int m_Quote;

    public AssetExistStatusEnum resourceExistStatus = AssetExistStatusEnum.Once;

    public UnityEngine.Object m_Asset;

    public ResourceStateObj(ResourceSettingStateObj stateObj)
    {
        this.m_Name = stateObj.m_Name;
        this.m_ResourcePath = stateObj.m_ResourcePath;
        this.m_AssetPath = stateObj.m_AssetPath;
        this.m_Type = stateObj.m_Type;
    }
}
[CreateAssetMenu(fileName = "ResourceLibarySetting", menuName = "LibaryResourceSetting")]
public class LibaryResourceSetting : ScriptableObject
{
    public List<ResourceSettingStateObj> texture2ds;
    public List<ResourceSettingStateObj> sprites;
    public List<ResourceSettingStateObj> textAssets;
    public List<ResourceSettingStateObj> audios;
    public List<ResourceSettingStateObj> videos;
    public List<ResourceSettingStateObj> renderTextures;
    public List<ResourceSettingStateObj> materials;
    public List<ResourceSettingStateObj> prefabs;
    public List<ResourceSettingStateObj> movieTextures;
    public List<ResourceSettingStateObj> fonts;
    public List<ResourceSettingStateObj> scriptableObjects;
    public List<ResourceSettingStateObj> runtimeAnimatorControllers;
    public void AddResToLibary(ResourceSettingStateObj resourceSetting)
    {
        LibaryTypeEnum libaryStatusEnum;
        if (!ResLibaryConfig.ExistTypeNameToEnum.TryGetValue(resourceSetting.m_Type, out libaryStatusEnum))
            return;
        switch (libaryStatusEnum)
        {
            case LibaryTypeEnum.LibaryType_Texture2D:
                if (texture2ds == null)
                    texture2ds = new List<ResourceSettingStateObj>();
                if (!texture2ds.Contains(resourceSetting))
                    texture2ds.Add(resourceSetting);
                break;

            case LibaryTypeEnum.LibaryType_TextAsset:
                if (textAssets == null)
                    textAssets = new List<ResourceSettingStateObj>();
                if (!textAssets.Contains(resourceSetting))
                    textAssets.Add(resourceSetting);
                break;

            case LibaryTypeEnum.LibaryType_Sprite:
                if (sprites == null)
                    sprites = new List<ResourceSettingStateObj>();
                if (!sprites.Contains(resourceSetting))
                    sprites.Add(resourceSetting);
                break;

            case LibaryTypeEnum.LibaryType_AudioClip:
                if (audios == null)
                    audios = new List<ResourceSettingStateObj>();
                if (!audios.Contains(resourceSetting))
                    audios.Add(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_VideoClip:
                if (videos == null)
                    videos = new List<ResourceSettingStateObj>();
                if (!videos.Contains(resourceSetting))
                    videos.Add(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_RenderTexture:
                if (renderTextures == null)
                    renderTextures = new List<ResourceSettingStateObj>();
                if (!renderTextures.Contains(resourceSetting))
                    renderTextures.Add(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_MovieTexture:
                if (movieTextures == null)
                    movieTextures = new List<ResourceSettingStateObj>();
                if (!movieTextures.Contains(resourceSetting))
                    movieTextures.Add(resourceSetting);
                break;

            case LibaryTypeEnum.LibaryType_Material:
                if (materials == null)
                    materials = new List<ResourceSettingStateObj>();
                if (!materials.Contains(resourceSetting))
                    materials.Add(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_GameObject:
                if (prefabs == null)
                    prefabs = new List<ResourceSettingStateObj>();
                if (!prefabs.Contains(resourceSetting))
                    prefabs.Add(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_Font:
                if (fonts == null)
                    fonts = new List<ResourceSettingStateObj>();
                if (!fonts.Contains(resourceSetting))
                    fonts.Add(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_ScriptableObject:
                if (scriptableObjects == null)
                    scriptableObjects = new List<ResourceSettingStateObj>();
                if (!scriptableObjects.Contains(resourceSetting))
                    scriptableObjects.Add(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_RuntimeAnimatorController:
                if (runtimeAnimatorControllers == null)
                    runtimeAnimatorControllers = new List<ResourceSettingStateObj>();
                if (!runtimeAnimatorControllers.Contains(resourceSetting))
                    runtimeAnimatorControllers.Add(resourceSetting);
                break;
        }
    }

    public  void DelResToLibary(ResourceSettingStateObj resourceSetting)
    {
        LibaryTypeEnum libaryStatusEnum;
        if (!ResLibaryConfig.ExistTypeNameToEnum.TryGetValue(resourceSetting.m_Type, out libaryStatusEnum))
            return;
        switch (libaryStatusEnum)
        {
            case LibaryTypeEnum.LibaryType_Texture2D:
                if (texture2ds == null)
                    texture2ds = new List<ResourceSettingStateObj>();
                if (texture2ds.Contains(resourceSetting))
                    texture2ds.Remove(resourceSetting);
                break;

            case LibaryTypeEnum.LibaryType_TextAsset:
                if (textAssets == null)
                    textAssets = new List<ResourceSettingStateObj>();
                if (textAssets.Contains(resourceSetting))
                    textAssets.Remove(resourceSetting);
                break;

            case LibaryTypeEnum.LibaryType_Sprite:
                if (sprites == null)
                    sprites = new List<ResourceSettingStateObj>();
                if (sprites.Contains(resourceSetting))
                    sprites.Remove(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_AudioClip:
                if (audios == null)
                    audios = new List<ResourceSettingStateObj>();
                if (audios.Contains(resourceSetting))
                    audios.Remove(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_VideoClip:
                if (videos == null)
                    videos = new List<ResourceSettingStateObj>();
                if (videos.Contains(resourceSetting))
                    videos.Remove(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_RenderTexture:
                if (renderTextures == null)
                    renderTextures = new List<ResourceSettingStateObj>();
                if (renderTextures.Contains(resourceSetting))
                    renderTextures.Remove(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_MovieTexture:
                if (movieTextures == null)
                    movieTextures = new List<ResourceSettingStateObj>();
                if (movieTextures.Contains(resourceSetting))
                    movieTextures.Remove(resourceSetting);
                break;


            case LibaryTypeEnum.LibaryType_Material:
                if (materials == null)
                    materials = new List<ResourceSettingStateObj>();
                if (materials.Contains(resourceSetting))
                    materials.Remove(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_GameObject:
                if (prefabs == null)
                    prefabs = new List<ResourceSettingStateObj>();
                if (prefabs.Contains(resourceSetting))
                    prefabs.Remove(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_Font:
                if (fonts == null)
                    fonts = new List<ResourceSettingStateObj>();
                if (fonts.Contains(resourceSetting))
                    fonts.Remove(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_ScriptableObject:
                if (scriptableObjects == null)
                    scriptableObjects = new List<ResourceSettingStateObj>();
                if (scriptableObjects.Contains(resourceSetting))
                    scriptableObjects.Remove(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_RuntimeAnimatorController:
                if (runtimeAnimatorControllers == null)
                    runtimeAnimatorControllers = new List<ResourceSettingStateObj>();
                if (runtimeAnimatorControllers.Contains(resourceSetting))
                    runtimeAnimatorControllers.Remove(resourceSetting);
                break;
        }
    }

    public Dictionary<string, Dictionary<string, ResourceSettingStateObj>> GetSettingMessage()
    {
        Dictionary<string, Dictionary<string, ResourceSettingStateObj>> dict = new Dictionary<string, Dictionary<string, ResourceSettingStateObj>>();
        if (texture2ds == null)
            texture2ds = new List<ResourceSettingStateObj>();
        if (textAssets == null)
            textAssets = new List<ResourceSettingStateObj>();
        if (audios == null)
            audios = new List<ResourceSettingStateObj>();
        if (videos == null)
            videos = new List<ResourceSettingStateObj>();
        if (sprites == null)
            sprites = new List<ResourceSettingStateObj>();      
        if (renderTextures == null)
            renderTextures = new List<ResourceSettingStateObj>();
        if (movieTextures == null)
            movieTextures = new List<ResourceSettingStateObj>();
        if (materials == null)
            materials = new List<ResourceSettingStateObj>();
        if (prefabs == null)           
            prefabs = new List<ResourceSettingStateObj>();
        if (fonts == null)
            fonts = new List<ResourceSettingStateObj>();
        if (scriptableObjects == null)
            scriptableObjects = new List<ResourceSettingStateObj>();
        if (runtimeAnimatorControllers == null)
            runtimeAnimatorControllers = new List<ResourceSettingStateObj>();
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_Texture2D]] = GetResourceSetting(texture2ds);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_TextAsset]] = GetResourceSetting(textAssets);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_AudioClip]] = GetResourceSetting(audios);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_VideoClip]] = GetResourceSetting(videos);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_Sprite]] = GetResourceSetting(sprites);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_RenderTexture]] = GetResourceSetting(renderTextures);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_MovieTexture]] = GetResourceSetting(movieTextures);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_Material]] = GetResourceSetting(materials);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_GameObject]] = GetResourceSetting(prefabs);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_Font]] = GetResourceSetting(fonts);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_ScriptableObject]] = GetResourceSetting(scriptableObjects);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_RuntimeAnimatorController]] = GetResourceSetting(runtimeAnimatorControllers);
        return dict;
    }
    protected Dictionary<string, ResourceSettingStateObj> GetResourceSetting(List<ResourceSettingStateObj> list)
    {

        if (list != null)
        {
            Dictionary<string, ResourceSettingStateObj> returnList = new Dictionary<string, ResourceSettingStateObj>();
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i] != null && !returnList.ContainsKey(list[i].m_Name))
                    returnList.Add(list[i].m_Name, list[i]);
            }
            return returnList;
        }
        return null;
    }
    public void Clear()
    {
        if (texture2ds == null)
            texture2ds = new List<ResourceSettingStateObj>();
        if (textAssets == null)
            textAssets = new List<ResourceSettingStateObj>();
        if (audios == null)
            audios = new List<ResourceSettingStateObj>();
        if (videos == null)
            videos = new List<ResourceSettingStateObj>();
        if (sprites == null)
            sprites = new List<ResourceSettingStateObj>();
       
        if (renderTextures == null)
            renderTextures = new List<ResourceSettingStateObj>();
        if (movieTextures == null)
            movieTextures = new List<ResourceSettingStateObj>();
        if (materials == null)
            materials = new List<ResourceSettingStateObj>();
        if (prefabs == null)
            prefabs = new List<ResourceSettingStateObj>();
        if (fonts == null)
            fonts = new List<ResourceSettingStateObj>();
        if (scriptableObjects == null)
            scriptableObjects = new List<ResourceSettingStateObj>();
        if (runtimeAnimatorControllers == null)
            runtimeAnimatorControllers = new List<ResourceSettingStateObj>();
        sprites.Clear();
        texture2ds.Clear();
        audios.Clear();
        videos.Clear();
        textAssets.Clear();
        renderTextures.Clear();
        sprites.Clear();
        materials.Clear();
        prefabs.Clear();
        movieTextures.Clear();
        fonts.Clear();
        scriptableObjects.Clear();
        runtimeAnimatorControllers.Clear();
    }
}
//}
